package com.bootdo.change.service.impl;

import java.util.Date;
import java.util.UUID;

import com.bootdo.message.domain.MessageDO;
import com.bootdo.message.service.MessageService;
import com.bootdo.oa.domain.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bootdo.change.entity.ChangeTask;
import com.bootdo.change.service.ChangeTaskService;
import com.bootdo.common.utils.AppR;
import com.bootdo.goods.domain.TGoodsAllocationDO;
import com.bootdo.goods.service.TGoodsAllocationService;
import com.bootdo.staff.domain.StaffUserDO;
import com.bootdo.staff.service.StaffUserService;

@Service
public class AutoChangeService {

	@Autowired
	private TGoodsAllocationService goodsAllocationService;
	@Autowired
	private ChangeTaskService changeTaskService;
	
	@Autowired
	private StaffUserService staffUserService;

	@Autowired
	private MessageService messageService;
	/***
	 * 自动调仓
	 * @param goodsId 商品ID
	 * @param goodsNumber 商品数量
	 * @param providerId 供应商ID
	 * @param orderId 订单编号
	 * @param quickId 加急订单
	 * @param taskId 任务编号
	 * @return
	 */
	public AppR autoChange(Long goodsId,Integer goodsNumber,Long providerId,Long orderId,String quickId,Integer taskId){
		TGoodsAllocationDO selectAll = goodsAllocationService.selectAll(goodsId,goodsNumber,providerId);
		TGoodsAllocationDO selectOne = goodsAllocationService.selectOne(providerId);
		if (selectAll!=null && selectOne!=null) {
			goodsAllocationService.updateOne("120004",selectAll.getAllocationId());//锁定初始仓位
			goodsAllocationService.updateOne("120004",selectOne.getAllocationId());//锁定目标仓位
			
			//找到将要分配调仓任务的叉车工
			StaffUserDO selectOne2 = staffUserService.selectOne();
			if (selectOne2!=null) {
				//第一次分配任务给叉车共
				String str = UUID.randomUUID().toString();
				ChangeTask ct = new ChangeTask();
				ct.setId(str);
				ct.setTdInitalAllocation(selectAll.getAllocationId());
				ct.setTdTargetAllocation(selectOne.getAllocationId());
				ct.setChangeNumber(selectAll.getGoodsNumber());
				ct.setTdStime(new Date());
				ct.setTdDoname(selectOne2.getStaffName());
				ct.setTdStatus(0);
				ct.setTdType(0);
				ct.setRoleId(selectOne2.getStaffId());
				ct.setProviderId(providerId);
				ct.setOrderId(orderId);
				if (quickId!=null) {//加急
					ct.setQuickId(1);
				}
				if (taskId!=null) {
					ct.setTaskId(taskId);
				}
				ct.setNeedNumber(goodsNumber);
				ct.setGoodsId(goodsId);
				ct.setAllocationNumberS(selectAll.getAllocationNumber());

				ct.setAllocationNumberE(selectOne.getAllocationNumber());
				changeTaskService.save(ct);
			}else{
				//根据需要调仓的供应商ID找到上一次执行该供应商的叉车工人
				ChangeTask selectOne3 = changeTaskService.selectOne(providerId,orderId);
				if (selectOne3 == null ) {
					ChangeTask selectOne4 = changeTaskService.selectByOne();
					String str = UUID.randomUUID().toString();
					ChangeTask ct = new ChangeTask();
					ct.setId(str);
					ct.setTdInitalAllocation(selectAll.getAllocationId());
					ct.setTdTargetAllocation(selectOne.getAllocationId());
					ct.setChangeNumber(selectAll.getGoodsNumber());
					ct.setTdStime(new Date());
					ct.setTdDoname(selectOne4.getTdDoname());
					ct.setTdStatus(0);
					ct.setTdType(0);
					ct.setRoleId(selectOne4.getRoleId());
					ct.setProviderId(providerId);
					ct.setOrderId(orderId);
					if (quickId!=null) {
						ct.setQuickId(1);;
					}
					if (taskId!=null) {
						ct.setTaskId(taskId);
					}
					ct.setNeedNumber(goodsNumber);
					ct.setGoodsId(goodsId);
					ct.setAllocationNumberS(selectAll.getAllocationNumber());
					ct.setAllocationNumberE(selectOne.getAllocationNumber());
					changeTaskService.save(ct);
				}else{
					String str = UUID.randomUUID().toString();
					ChangeTask ct = new ChangeTask();
					ct.setId(str);
					ct.setTdInitalAllocation(selectAll.getAllocationId());
					ct.setTdTargetAllocation(selectOne.getAllocationId());
					ct.setChangeNumber(selectAll.getGoodsNumber());
					ct.setTdStime(new Date());
					ct.setTdDoname(selectOne3.getTdDoname());
					ct.setTdStatus(0);
					ct.setTdType(0);
					ct.setRoleId(selectOne3.getRoleId());
					ct.setProviderId(providerId);
					ct.setOrderId(orderId);
					if (quickId!=null) {
						ct.setQuickId(1);;
					}
					if (taskId!=null) {
						ct.setTaskId(taskId);
					}
					ct.setNeedNumber(goodsNumber);
					ct.setGoodsId(goodsId);
					ct.setAllocationNumberS(selectAll.getAllocationNumber());
					ct.setAllocationNumberE(selectOne.getAllocationNumber());
					changeTaskService.save(ct);
				}
				
			}
			
		}else{
			//发消息给主管，提示去及时处理
			MessageDO messageDo = new MessageDO();
			messageDo.setMessageTitle("调仓异常");
			messageDo.setMessageContent("自动调仓仓位不足，无法继续调仓，订单"+orderId+"无法继续分拣出库，为防止影响后续订单出库，请尽快处理！");
			messageDo.setCreateTime(new Date());
			messageDo.setIsRead("001");
			messageService.save(messageDo);
			return AppR.error("未找到空仓位");
		}
		
		return AppR.ok();
	}
}
